package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.PermissionDao;
import com.itheima.health.dao.RoleDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.pojo.Permission;
import com.itheima.health.pojo.Role;
import com.itheima.health.pojo.User;
import com.itheima.health.service.PermissionService;
import com.itheima.health.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName CheckItemServiceImpl
 * @Description TODO
 * @Author ly
 * @Company 深圳黑马程序员
 * @Date 2019/10/23 15:57
 * @Version V1.0
 */
@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    RoleDao roleDao;




    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        // 1：完成对分页初始化工作
        PageHelper.startPage(currentPage,pageSize);
        // 2：查询
        Page<Role> page = roleDao.findPage(queryString);
        // 组织PageResult
        return new PageResult(page.getTotal(),page.getResult());


    }

    @Override
    public void deleteById(Integer id) {

        // 使用检查项id，查询检查项
        Integer count = roleDao.findCheckGroupAndCheckItemCountByCheckItemId(id);
        if (count>0){
            throw new RuntimeException("当前检查项和检查组之间存在关联关系，不能删除");
        }
        roleDao.deleteById(id);
    }

    @Override
    public Role findById(Integer id) {
        return roleDao.findById(id);
    }



    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }



    @Override
    public void add(Role role, Integer[] permissionIds, Integer[] menuIds) {


            // 1：组织角色的表单数据，保存
            roleDao.add(role);

            setCheckGroupAndCheckItem(role.getId(),permissionIds);
            setRoleAndMenu(role.getId(),menuIds);
        }

    @Override
    public void edit22(Role role, Integer[] permissionIds, Integer[] menuIds) {
        roleDao.edit22(role);
        roleDao.deleteRoleAndPermission(role.getId());
        roleDao.deleteRoleAndMenu(role.getId());


        setCheckGroupAndCheckItem(role.getId(),permissionIds);
        setRoleAndMenu(role.getId(),menuIds);
    }

    private void setRoleAndMenu(Integer roleId, Integer[] menuIds) {

        if(menuIds!=null && menuIds.length>0){
            for (Integer menuId : menuIds) {
                // 方案一：在CheckGroupDao中@Param
                // checkGroupDao.addCheckGroupAndCheckItem(checkGroupId,checkItemId);
                // 方案二：使用Map集合
                Map<String,Object> map = new HashMap<>();
                map.put("role_Id",roleId);
                map.put("menu_Id",menuId);
                roleDao.addRoleAndMenu(map);
            }
        }
    }


    // 遍历页面传递过来的checkitemIds数组，向检查组和检查项的中间表中插入数据
        private void setCheckGroupAndCheckItem(Integer roleId, Integer[] permissionIds) {
            if(permissionIds!=null && permissionIds.length>0){
                for (Integer permissionId : permissionIds) {
                    // 方案一：在CheckGroupDao中@Param
                    // checkGroupDao.addCheckGroupAndCheckItem(checkGroupId,checkItemId);
                    // 方案二：使用Map集合
                    Map<String,Object> map = new HashMap<>();
                    map.put("role_Id",roleId);
                    map.put("permission_Id",permissionId);
                    roleDao.addRoleAndPermission(map);
                }
            }
        }


    // roleDao.deleteRoleAndPermission(role.getId());
    //        roleDao.deleteRoleAndMenu(role.getId());


}
